﻿using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using SidebarStarted.Models;
using System;
using System.Collections.ObjectModel;
using System.Linq;

namespace SidebarStarted.ViewModels;

public partial class MainWindowViewModel : ViewModelBase
{
    public MainWindowViewModel()
    {
        // CurrentPage = new HomePageViewModel();
        IsPanelOpen = true;
        SelectedListItem = MenuBars.First(x => x.ModelType == typeof(HomePageViewModel));
    }

    /// <summary>
    /// 是否打开
    /// </summary>
    [ObservableProperty]
    private bool _isPanelOpen;

    /// <summary>
    /// 当前页面
    /// </summary>
    [ObservableProperty]
    private ViewModelBase _currentPage;

    /// <summary>
    /// 列表选择项
    /// </summary>
    [ObservableProperty]
    private MenuBar? _selectedListItem;

    /// <summary>
    /// 菜单内容
    /// </summary>
    public ObservableCollection<MenuBar> MenuBars { get; } =
    [
        new MenuBar("主页", typeof(HomePageViewModel), "HomeRegular"),

        //new MenuBar("证件照换背景", typeof(IdentityPhotoPageViewModel), "ImageRegular"),
        new MenuBar("测试1", typeof(TestOnePageViewModel),
            "TextNumberFormatRegular"),
        new MenuBar("测试2", typeof(TestTwoPageViewModel), "ImageRegular")
    ];

    partial void OnSelectedListItemChanged(MenuBar? value)
    {
        if (value is null)
            return;

        // 不使用ioc的方式
        // var instance = Activator.CreateInstance(value.ModelType);
        // 使用ioc的方式
        var instance = Design.IsDesignMode ? Activator.CreateInstance(value.ModelType) : Ioc.Default.GetService(value.ModelType);
        if (instance is null)
            return;

        CurrentPage = (ViewModelBase)instance;
    }

    [RelayCommand]
    private void TriggerPane()
    {
        IsPanelOpen = !IsPanelOpen;
    }
}